package com.kzd.gtgame.main.game

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.setEmptyView
import com.kzd.gtgame.custom.setTransitionStatusT
import com.kzd.gtgame.entity.GameBean
import com.kzd.gtgame.jetpack.vm.GameViewModel
import com.kzd.gtgame.main.game.adapter.GameFeaturedAdapter
import com.kzd.gtgame.main.game.adapter.GameFeaturedSlideAdapter
import com.kzd.gtgame.main.toGameDetailsActivity
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeFragment
import com.youth.banner.Banner
import kotlinx.android.synthetic.main.include_app_refresh.*

class GameFeaturedFragment : OwnerViewModeFragment<GameViewModel>() {

    companion object {
        fun newInstance(): GameFeaturedFragment {
            return GameFeaturedFragment()
        }
    }

    private val mGameFeaturedAdapter = GameFeaturedAdapter()

    override fun viewModel(): GameViewModel {
        return createViewModel(requireParentFragment())
    }

    override val layoutResId: Int
        get() = R.layout.include_app_refresh

    override fun initView() {

        rvAppRefreshBody.apply {
            layoutManager = LinearLayoutManager(requireContext())
        }.adapter = mGameFeaturedAdapter.apply {
            setOnItemClickListener { _, _, position ->
                val itemData = mGameFeaturedAdapter.data[position]
                viewModel.selectedClassifyIdLiveData.postValue(
                    GameViewModel.GameUpdate(itemData.id)
                )
            }
        }

        mSmartRefreshLayout.setEnableLoadMore(false)
        mSmartRefreshLayout.setOnRefreshListener {
            viewModel.gameFeatured()
        }
    }

    override fun initData() {
        viewModel.gameFeatured()
    }

    override fun initViewMode() {

        viewModel.gameFeaturedLiveData.observe(viewLifecycleOwner, { response ->
            setTransitionStatusT(response, mGameFeaturedAdapter, mSmartRefreshLayout, (false))
            if (response.status != Status.SUCCESS)
                return@observe
            val gameFeatured = response.data?.data
                ?: return@observe
            if (gameFeatured.slide.isNullOrEmpty() && gameFeatured.game.isNullOrEmpty()) {
                mGameFeaturedAdapter.setEmptyView()
            }
            if (!gameFeatured.slide.isNullOrEmpty()) {
                mGameFeaturedAdapter.setHeaderView(
                    createFeaturedSlideView(gameFeatured.slide)
                )
            }
            if (!gameFeatured.game.isNullOrEmpty()) {
                mGameFeaturedAdapter.setNewInstance(gameFeatured.game)
            }
        })
    }

    private fun createFeaturedSlideView(games: MutableList<GameBean>): View =
        Banner<GameBean, GameFeaturedSlideAdapter>(requireContext()).apply {
            setOrientation(Banner.HORIZONTAL)
            setUserInputEnabled(true)
            adapter = GameFeaturedSlideAdapter(games)
            setOnBannerListener { data, _ ->
                if (data !is GameBean)
                    return@setOnBannerListener
                val gid = data.id
                    ?: return@setOnBannerListener
                toGameDetailsActivity(gid)
            }
        }
}